package com.luomu.base.api.utils;


import com.luomu.base.api.bo.ReqInfo;
import org.apache.commons.collections4.CollectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class AssignBasicFieldUtil {
    public AssignBasicFieldUtil() {
    }

    public static void copyAssignBasicField(Object sourceBO, Object destinationObject, boolean bcreate) {
        if (sourceBO instanceof ReqInfo) {
            ReqInfo sourceReqInfo = (ReqInfo)sourceBO;

            try {
                List<Method> listMethod = getMethods(destinationObject.getClass());
                if (CollectionUtils.isEmpty(listMethod)) {
                    return;
                }

                Class<?> clazz = destinationObject.getClass();
                Iterator var6 = listMethod.iterator();

                Method m;
                Field createUserField;
                while(var6.hasNext()) {
                    m = (Method)var6.next();
                    if ("getOrgId".equals(m.getName())) {
                        createUserField = clazz.getDeclaredField("orgId");
                        createUserField.setAccessible(true);
                        createUserField.set(destinationObject, sourceReqInfo.getOrgId());
                    }

                    if ("getAppId".equals(m.getName())) {
                        createUserField = clazz.getDeclaredField("appId");
                        createUserField.setAccessible(true);
                        createUserField.set(destinationObject, sourceReqInfo.getAppId());
                    }

                    if ("getGroupId".equals(m.getName())) {
                        createUserField = clazz.getDeclaredField("groupId");
                        createUserField.setAccessible(true);
                        createUserField.set(destinationObject, sourceReqInfo.getGroupId());
                    }

                    if ("getAgentId".equals(m.getName())) {
                        createUserField = clazz.getDeclaredField("agentId");
                        createUserField.setAccessible(true);
                        createUserField.set(destinationObject, sourceReqInfo.getAgentId());
                    }
                }

                var6 = listMethod.iterator();

                while(var6.hasNext()) {
                    m = (Method)var6.next();
                    if (bcreate) {
                        if ("getCreateTime".equals(m.getName())) {
                            createUserField = clazz.getDeclaredField("createTime");
                            createUserField.setAccessible(true);
                            createUserField.set(destinationObject, new Date());
                        }

                        if ("getCreateUserId".equals(m.getName())) {
                            createUserField = clazz.getDeclaredField("createUserId");
                            createUserField.setAccessible(true);
                            createUserField.set(destinationObject, sourceReqInfo.getOpenId());
                        }

                        if ("getCreateAgentId".equals(m.getName())) {
                            createUserField = clazz.getDeclaredField("createAgentId");
                            createUserField.setAccessible(true);
                            createUserField.set(destinationObject, sourceReqInfo.getAgentId());
                        }

                        if ("getIsDelete".equals(m.getName())) {
                            createUserField = clazz.getDeclaredField("isDelete");
                            createUserField.setAccessible(true);
                            createUserField.set(destinationObject, 0);
                        }

                        if ("getCreateUser".equals(m.getName())) {
                            createUserField = clazz.getDeclaredField("createUser");
                            createUserField.setAccessible(true);
                            createUserField.set(destinationObject, sourceReqInfo.getPersonName());
                        }
                    } else {
                        if ("getUpdateTime".equals(m.getName())) {
                            createUserField = clazz.getDeclaredField("updateTime");
                            createUserField.setAccessible(true);
                            createUserField.set(destinationObject, new Date());
                        }

                        if ("getUpdateUserId".equals(m.getName())) {
                            createUserField = clazz.getDeclaredField("updateUserId");
                            createUserField.setAccessible(true);
                            createUserField.set(destinationObject, sourceReqInfo.getOpenId());
                        }

                        if ("getUpdateAgentId".equals(m.getName())) {
                            createUserField = clazz.getDeclaredField("updateAgentId");
                            createUserField.setAccessible(true);
                            createUserField.set(destinationObject, sourceReqInfo.getAgentId());
                        }

                        if ("getUpdateUser".equals(m.getName())) {
                            createUserField = clazz.getDeclaredField("updateUser");
                            createUserField.setAccessible(true);
                            createUserField.set(destinationObject, sourceReqInfo.getPersonName());
                        }
                    }
                }
            } catch (NoSuchFieldException var9) {
                NoSuchFieldException e = var9;
                e.printStackTrace();
            } catch (IllegalAccessException var10) {
                IllegalAccessException e = var10;
                e.printStackTrace();
            }

        }
    }

    public static List<Method> getMethods(Class<?> clazz) {
        List<Method> list = new ArrayList();
        Method[] methods = clazz.getDeclaredMethods();
        Method[] var3 = methods;
        int var4 = methods.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            Method method = var3[var5];
            list.add(method);
        }

        return list;
    }
}